package com.log4jviewer.ui.preferences.filters;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.junit.Test;

/**
 * 
 * @author <a href="mailto:Daniil.Yaroslavtsev@gmail.com"> Daniil Yaroslavtsev </a>
 * 
 */
public class FilterRuleTableTest extends AbstractFiltersPageTest {

    protected static final SWTWorkbenchBot BOT = getBot();

    /**
     * Tests that FilterRulesTable first cell can`t be edit by F2 key or mouseclick.
     * 
     * @throws InterruptedException
     */
    @Test
    public void testFilterRuleTableFirstCellNotEditable() {

        TableViewer filtersRulesViewer = getFilterRuleSettings().getTableViewer();

        addFilter();

        getFilterTable().select(0);
        addFilterRule();

        // try to edit first cell by F2 key
        getFilterRuleTable().pressShortcut(getKey(SWT.F2));
        waitForCellEditorState(filtersRulesViewer, false);
        boolean isCellEditorActive = filtersRulesViewer.isCellEditorActive();
        assertFalse(isCellEditorActive);

        // try to edit first cell by mouse doubleclick
        getFilterRuleTable().doubleClick(0, 0);
        waitForCellEditorState(filtersRulesViewer, false);
        isCellEditorActive = filtersRulesViewer.isCellEditorActive();
        assertFalse(isCellEditorActive);
    }

    @Test
    public void testFilterRuleTableEditingByF2Key() {
        TableViewer filtersRulesViewer = getFilterRuleSettings().getTableViewer();
        addFilter();
        getFilterTable().select(0);
        addFilterRule();
        getFilterRuleTable().click(0, 1);
        getFilterRuleTable().pressShortcut(getKey(SWT.F2));
        waitForCellEditorState(filtersRulesViewer, true);
        boolean isCellEditorActive = filtersRulesViewer.isCellEditorActive();
        assertTrue(isCellEditorActive);

    }

    @Test
    public void filterRuleTableEditingByMouseDoubleClick() {
        TableViewer filtersRulesViewer = getFilterRuleSettings().getTableViewer();
        addFilter();
        getFilterTable().select(0);
        addFilterRule();
        getFilterRuleTable().doubleClick(0, 1);
        waitForCellEditorState(filtersRulesViewer, true);
        boolean isCellEditorActive = filtersRulesViewer.isCellEditorActive();
        assertTrue(isCellEditorActive);
    }

    /**
     * Tests that FilterRulesTable ENABLE_COLUMN`s cells can`t be edit (by F2 key or mouseclick). *
     */
    @Test
    public void testFilterRuleTableEnableColumnIsNotEditable() {

        TableViewer filtersRulesViewer = getFilterRuleSettings().getTableViewer();

        addFilter();
        getFilterTable().select(0);
        addFilterRule();
        int enableColumnIndex = FilterRuleSettings.ENABLE_COLUMN_INDEX;

        for (int i = 0; i < getFilterRuleTable().rowCount(); i++) {
            // try to edit the cell by F2 key
            getFilterRuleTable().click(i, enableColumnIndex);
            getFilterRuleTable().pressShortcut(getKey(SWT.F2));
            waitForCellEditorState(filtersRulesViewer, false);
            boolean isCellEditorActive = filtersRulesViewer.isCellEditorActive();
            assertFalse(isCellEditorActive);

            // try to edit the cell by mouse doubleclick
            getFilterRuleTable().doubleClick(i, enableColumnIndex);
            waitForCellEditorState(filtersRulesViewer, false);
            isCellEditorActive = filtersRulesViewer.isCellEditorActive();
            assertFalse(isCellEditorActive);
        }
    }

}
